package cn.itcast.item.service.impl;

import cn.itcast.hmall.dto.common.PageDTO;
import cn.itcast.hmall.dto.item.SearchItemDTO;
import cn.itcast.hmall.pojo.item.Item;
import cn.itcast.item.mapper.ItemMapper;
import cn.itcast.item.service.ItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageDTO<Item> queryItemByPage(SearchItemDTO searchItemDTO) {
        Integer page = searchItemDTO.getPage();
        Integer size = searchItemDTO.getSize();
        //构建分页条件
        Page<Item> itemPage = new Page<>(page, size);
        //构建查询条件
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        itemMapper.selectPage(itemPage, queryWrapper);
        long total = itemPage.getTotal();
        List<Item> records = itemPage.getRecords();

        // 构建响应对象
        PageDTO<Item> result = new PageDTO<>(total, records);
        return result;
    }
}
